package com.itheima.aspect;


import com.itheima.entity.Emp;
import com.itheima.entity.EmpLoginLog;
import com.itheima.entity.FileUploadLog;
import com.itheima.entity.Result;
import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.mapper.UploadFileMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

@Component
//@Aspect
public class EmpLog {
    @Autowired
    UploadFileMapper uploadFileMapper;

    // login操作，获取令牌，直接从请求头获取令牌：登录操作
    @Around("@annotation(com.itheima.annotation.MyLog)")
    public Object empLoginLog(ProceedingJoinPoint pjp) throws Throwable { //连接点：目标方法
        //1.执行目标
        long begin = System.currentTimeMillis();
        Object result = pjp.proceed(); //执行登录方法
        long end = System.currentTimeMillis();
        //获取目标方法的参数对象
        Object[] args = pjp.getArgs();
        MultipartFile file = (MultipartFile) args[0];


        // 2.记录登录日志
        //2.1封装日志对象
        FileUploadLog fileUploadLog = new FileUploadLog();
        fileUploadLog.setUploadTime(LocalDateTime.now());
        fileUploadLog.setFilename(file.getOriginalFilename());
        fileUploadLog.setExtname(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1));
        fileUploadLog.setSize(file.getSize());


        Result uplogResylt = (Result) result;
        fileUploadLog.setUrl((String) uplogResylt.getData());


        // 调用持久层
        uploadFileMapper.insert(fileUploadLog);
        return result;
    }
}

